<%@page import="com.mbbcpa.util.StringUtils"%>
<%@ page import = "java.io.*" %>
<%@ page import = "java.net.*" %>
<%@ page import = "java.text.*" %>
<%@ page import = "java.util.*" %>
<%@ page import = "java.util.HashMap" %>

<%@page import="com.mbbcpa.reference.RefListModel"%>
<%@ page import = "java.util.Map" %>
<%@ page import = "java.util.Vector" %>
<%@ page import="com.mbbcpa.app.manager.*" %>
<%@ page import="com.mbbcpa.app.EncryptionHelper" %>
<%@ page import="com.mbbcpa.app.EbppKeys" %>
<%@ page import="com.mbbcpa.util.*" %>
<%@ page import = "com.mbbcpa.security.dao.*" %>
<%@ page import = "com.mbbcpa.app.dao.*" %>
<%

// -- Loading Constants
	
	Map map = null;
	Vector vLosRefList = null;
	Vector vCountryRefList = null;
	Vector vFinTypeRefList = null;
	Vector vBorrowerDetails = null;
	Vector vFacilityInfo = null;
	Vector vFacilityDetails = null;
	LoanInfoDao loanInfoDao = null;
	Boolean isNew = true;
	
	//dataCount
	int borrowerCount = 0;
	int facilityCount = 0;
	int loanSecurityCount = 0;
	
	
	String countryCode = "";
	String losSrc = "";
	String finType = "";
	String aaNo = "";
	
	map = (Map) request.getAttribute("map"); 
	vLosRefList = (Vector) map.get("vLosRefList");
	vCountryRefList = (Vector) map.get("vCountryRefList");
	vFinTypeRefList = (Vector) map.get("vFinTypeRefList");
	loanInfoDao = (LoanInfoDao) map.get("loanInfoDao");	
	
	
	//check manualcp is newly loaded through the loanInfoDao
	if (loanInfoDao != null){
		
		isNew = false;
		
		// Loan Info
		countryCode = loanInfoDao.getCountryCode();
		losSrc = loanInfoDao.getLOSSrc();
		finType = loanInfoDao.getEntity();
		aaNo = loanInfoDao.getAANo();
		
		//FacilityInfo
		vFacilityInfo = (Vector) map.get("vFacilityInfo");
		if (vFacilityInfo != null){
			facilityCount = vFacilityInfo.size();
		}
		
		
		// Borrower Details
		vBorrowerDetails = (Vector) map.get("vBorrowerDetails");
		if (vBorrowerDetails != null){
			borrowerCount = vBorrowerDetails.size();
		}
		
	
		
	}
		
	
// -- Navigation

	// Get Manual CP Acces List
	ArrayList secGroupAccessList = (ArrayList) session.getAttribute(Constants.LOGON_ACCESS_LIST);
	
	// Get Login Details 
	SecUserDao oSecUserDao = (SecUserDao) session.getAttribute(Constants.LOGGED_USER);
	String sUserName = oSecUserDao.getUserName();
	String lastLogonDateTime = (String) session.getAttribute(Constants.LAST_LOGON_DATETIME);
	
	// Module Declaration
	int iMan001 = 0;
	int iMan002 = 0;
	
	for(int i=0;i<secGroupAccessList.size();i++){
		SecAccessDao secaccessDao = (SecAccessDao)secGroupAccessList.get(i);
		String strModuleCode  = secaccessDao.getModuleCode();
	
		if(strModuleCode.equals("MAN001"))
			iMan001 = 1;
		if(strModuleCode.equals("MAN002"))
			iMan002 = 1;
	}

%>

<html>
<head>
<link rel="stylesheet" href="resource/mbb_sec_global.css" type="text/css" />
<link href="resource/style.css" rel="stylesheet" type="text/css">


<SCRIPT LANGUAGE="JavaScript1.2">


	
	function checkInquiry() {
		window.history.forward(); 

	}
	

	function validateLoanInfo(){
		
		var isValid = false;
		var frm = document.frmManualCP;
		
		if (frm.countryCode.value == ""){
			alert("Please select a Country Code");
			frm.countryCode.focus();
		}else if (frm.finType.value == ""){
			alert("Please select an Entity");
			frm.finType.focus();		
		}else if(frm.losSrc.value == ""){ 
			alert("Please select a LOS Source");
			frm.losSrc.focus();	
		}else if(frm.aaNo.value == ""){
			alert("Please input AA No.");
			frm.aaNo.focus();		
		}
		else{
			isValid = true;
		}

		return isValid; 
	}
	
	
	function showBorrowerForm(){
		var frm = document.frmManualCP;
		//call validation function
		var isValid = validateLoanInfo();
		
		//alert (isValid);
			//validation success? put loan info in vector
			if (isValid == true){
				frm.action = "ManualCPInquiry.do?dispatch=addBorrowerDetails";
				frm.submit();
			}
	}
	
	
	function showLoanSecurity(facilityIndex){
		var frm = document.frmManualCP;
		//call validation function
		var isValid = validateLoanInfo();
		
		//alert (isValid);
			//validation success? put loan info in vector
			if (isValid == true){
				frm.action = "ManualCPInquiry.do?dispatch=addLoanSecurity&facilityIndex=" + facilityIndex;
				frm.submit();
			}
	}
	
	function showFacilityForm(){
		var frm = document.frmManualCP;
		//call validation function
		var isValid = validateLoanInfo();
		
		//alert (isValid);
			//validation success? put loan info in vector
			if (isValid == true){
				frm.action = "ManualCPInquiry.do?dispatch=addFacilityInfo";
				frm.submit();
			}
	}
	
	function deleteData(index, table){
		var frm = document.frmManualCP;
		var msg = "Are you sure you want to delete this entry?";
		
		
		if (confirm(msg)){
			var frm = document.frmManualCP;
			frm.action = 'ManualCPInquiry.do?dispatch=deleteData&index=' + index + '&table=' + table;
			frm.submit();	
			//alert ("Delete element " + index + " from " + table );
		}
		
	}
	
	
	function deleteLoanData(facilityIndex, loanSecurityIndex, table){
		
		var frm = document.frmManualCP;
		var msg = "Are you sure you want to delete this entry?";
		
		if (confirm(msg)){
		frm.action = 'ManualCPInquiry.do?dispatch=deleteData&facilityIndex=' + facilityIndex + '&loanSecurityIndex=' + loanSecurityIndex + '&table=' + table;
		frm.submit();
		}
	}
	
	function editData(index, table){

		var frm = document.frmManualCP;
		
		frm.action = 'ManualCPInquiry.do?dispatch=editData&index=' + index + '&table=' + table;
		frm.submit();		
	}
	
	function editLoanData(facilityIndex, loanSecurityIndex, table){

		var frm = document.frmManualCP;
		
		frm.action = 'ManualCPInquiry.do?dispatch=editData&facilityIndex=' + facilityIndex + '&loanSecurityIndex=' + loanSecurityIndex + '&table=' + table;
		frm.submit();		
	}
	
	
	function submitInquiry(){
		var frm = document.frmManualCP;
		//call validation function
		var isValid = validateLoanInfo();
		
			//validation success? put loan info in vector
			if (isValid == true){
				frm.action = "ManualCPInquiry.do?dispatch=submitManualInquiry";
				frm.submit();
			}
	}
	
</SCRIPT>
</head>


<body class="home" onpageshow="if (event.persisted) noBack();" onload="checkInquiry()" onunload="">

	<a name="top"></a>
	
<!-- Header -->
<%@ include file="/jsp/common/Header.jsp" %>	
<!-- End Header -->

<div id="bgWrapper">
		<div id="container">
		
					<div id="titleBar">
						<ul class="topBar">
							<li class="goBackBt"><a href="<%=request.getContextPath()%>/ShowManualCPHome.do?dispatch=init"></a></li>

							<li>
							  <h1>CP Inquiry</h1></li>
						</ul>
					</div>
		
					<div id="fullcontent">
		
						<!-- Security Navigation -->
						<%@ include file="ManualCPNavigation.jsp" %>	
						<!-- End SecurityNavigation -->
			
			
						<div id="contentRightIn">
									
							<div id="moduleContainer">
							
							
									<!--  Module Title -->
									<table align="center" width=100% border="0" rules="cols" class="tblDetails"  style="margin-top:-.1%">
										<thead>
											<tr>
												<th colspan="4">
													<h4>CP Inquiry</h4>
												</th>
											</tr>
										</thead>
									</table>


									<!-- Loan Info -->
													
										<table align="center" class="tblDetails" width=100% border="0" rules="cols">
											<form name="frmManualCP" method="post">	
											
											<thead class="th1">
												<tr>
													<th colspan="4">
														<div class="frmTitle">
															<h2 class="manualCPFormTitle">Loans Info</h2>
														</div>
													</th>
												</tr>
											</thead>
												
											<tbody>
												 <tr class="even">
											        <td width="25%">Country Code:</td>
											        <td width="25%">
											        	<select name="countryCode">
											          		<%
												          		for(int j = 0; j < vCountryRefList.size(); j++){
												          			RefListModel RefListModel = (RefListModel) vCountryRefList.elementAt(j);
												          			
												          			if ((isNew == false) && (countryCode.equals(RefListModel.get_rlt_value()))){
												          				//add selected option
												          				%>
												          					<option selected="selected" value="<%=RefListModel.get_rlt_value()%>">
														          		<%
												          			}else if(RefListModel.get_rlt_value().equals("MY")){ //add option only %>
												          					<option selected="selected" value="<%=RefListModel.get_rlt_value()%>">
																		<%
												          			}else{ //add option only %>
												          					<option value="<%=RefListModel.get_rlt_value()%>">
											          					<% } %>
												          			<%=RefListModel.get_rlt_value()%> 
											          				</option>
											          			<% } %>	     		
											          	</select>
											        </td>
											        <td width="25%">Entity:</td>
											        <td width="25%">
											        
											        	<select name="finType">
												        	<option value="">-</option>
												          		<%
												          		for(int j = 0; j < vFinTypeRefList.size(); j++){
												          			RefListModel RefListModel = (RefListModel) vFinTypeRefList.elementAt(j);
												          			
												          			if ((isNew == false) && (finType.equals(RefListModel.get_rlt_value()))){
												          				//add selected option
												          				%>
												          					<option selected="selected" value="<%=RefListModel.get_rlt_value()%>">
														          		<%
												          			}else{ //add option only %>
												          					<option value="<%=RefListModel.get_rlt_value()%>">
											          					<% 
												          			}
												          			%>
												          			<%=RefListModel.get_rlt_value()%> 
											          				</option>
											          			<% } %>											          
											      		</select>
											        
											        </td>
											      </tr>
											      
											      <tr class="odd">
											        <td width="25%">LOS Source:</td>
											        <td width="25%">											
														<select name="losSrc">
											        		<option value="">-</option>
											          														          		<%
												          		for(int j = 0; j < vLosRefList.size(); j++){
												          			RefListModel RefListModel = (RefListModel) vLosRefList.elementAt(j);
												          			
												          			if ((isNew == false) && (losSrc.equals(RefListModel.get_rlt_value()))){
												          				//add selected option
												          				%>
												          					<option selected="selected" value="<%=RefListModel.get_rlt_value()%>">
														          		<%
												          			}else{ //add option only %>
												          					<option value="<%=RefListModel.get_rlt_value()%>">
											          					<% 
												          			}
												          			%>
												          			<%=RefListModel.get_rlt_value()%> 
											          				</option>
											          			<% } %>	
													     </select>							        	
											        </td>
											        <td width="25%">AA NO. :</td>
											        <td width="25%">
											        	<% if (isNew == false){ %>
											        		<input name="aaNo" type="text" value="<%=loanInfoDao.getAANo()%>" />
											        	<% }else{ %>
											        		<input name="aaNo" type="text" />
											        	<% } %>
											        </td>
											        
											      </tr>  
											</tbody>
										</table>

										<table class="tblDetails" width="100%">
											
											  <tr class="even">
											        <td style="text-align:right !important;	">
											        	<input class="inputButton" name="btnaddBorrower" onClick="showBorrowerForm()" type="button" value="Add Borrower Details" />
											        	<input class="inputButton" name="btnaddFacility" onClick="showFacilityForm()" type="button" value="Add Facility Info" />
											        	<input class="inputButton" type="button" value="Submit Inquiry" onClick="submitInquiry()" />
											        </td>
											     </tr>
								
											
										</table>
									
									<!-- Facility Info Listing-->			
										<table align="center" class="tblDetails" width=100% border="0" rules="cols" <%=vFacilityInfo != null?"":"style=\"display:none\"" %>>
											<thead class="th1">
												<tr>
													<th colspan="10">
														<div class="frmTitle">
															<h2 class="manualCPFormTitle">Facility Info Listing</h2>
														</div>
													</th>
												</tr>
											</thead>
											
											<tbody>
												<!-- Detail Titles -->
											    <tr class="headerD" style="text-align:center !important;">
													<td width="5%" class="center_note">No.</td>
													<td width="15%" class="center_note">Facility ID</td>
													<td width="15%" class="center_note">Facility Type Code</td>
													<td width="15%" class="center_note">Product Type Code</td>
													<td width="15%" class="center_note">Purpose of Loan</td>
													<td width="15%" class="center_note">Loan Amount</td>
													<td width="15%" class="center_note">Branch Code</td>
													<td width="15%" class="center_note">Loan Tenure</td>
													<td width="15%" class="center_note" colspan="2">Action</td>
												</tr>  
												
												<!-- FacilityInfo Rows -->
													<% 
												
													if(isNew == true){
							
													}else{
														
														if (facilityCount > 0 ){
															for(int j = 0; j < vFacilityInfo.size(); j++){
																FacilityInfoDao facilityInfo = (FacilityInfoDao) vFacilityInfo.elementAt(j);
																String rowclass = ((j % 2) == 0 ? "even":"odd");		
																
																//Get Loan Security Details
																Vector vLoanSecurity = facilityInfo.getvLoanSecurity();
																if (vLoanSecurity != null){
																	loanSecurityCount = vLoanSecurity.size();
																}
													%>
															<tr class="<%=rowclass%>">
															 
																<td><input name="facilityIndex" type="hidden" value="<%=j%>" />  <%=j+1%></td>
																<td><input name="<%="facilityId" + (j + 1) %>" type="hidden" value="<%=facilityInfo.getFacId()%>" /><%=facilityInfo.getFacId()%></td>
																<td><input name="<%="facilityTypeCode" + (j + 1) %>" type="hidden" value="<%=facilityInfo.getFacTypeCode()%>" /><%=facilityInfo.getFacTypeCode()%></td>
																<td><input name="<%="productTypeCode" + (j + 1) %>" type="hidden" value="<%=facilityInfo.getFacProdCode()%>" /><%=facilityInfo.getFacProdCode()%></td>
																<td><input name="<%="purposeOfLoan" + (j + 1) %>" type="hidden" value="<%=facilityInfo.getFacPOL()%>" /><%=facilityInfo.getFacPOL()%></td>
																<td><input name="<%="loanAmount" + (j + 1) %>" type="hidden" value="<%=facilityInfo.getLoanAmount()%>" /><%=facilityInfo.getLoanAmount()%></td>
																<td><input name="<%="branchCode" + (j + 1) %>" type="hidden" value="<%=facilityInfo.getBranchCode()%>" /><%=facilityInfo.getBranchCode()%></td>
																<td><input name="<%="loanTenure" + (j + 1) %>" type="hidden" value="<%=facilityInfo.getLoanTenure()%>" /><%=facilityInfo.getLoanTenure()%>
																<input name="<%="facilityDateTime" + (j + 1) %>" type="hidden" value="<%=facilityInfo.getDateTime()%>" />
																<input name="<%="loanSecurityCount" + (j + 1) %>" type="hidden" value="<%=loanSecurityCount%>" /></td>
																
																<td>
																	<a href="javascript:showLoanSecurity(<%=j%>)" >Add Loan Security</a>
																</td>
																
																<td>
																	<a href="javascript:editData(<%=j%>, 'facilityInfo')" >Edit</a>
																	<a href="javascript:deleteData(<%=j%>, 'facilityInfo')" >Delete</a>
																</td>
															</tr>
															
															<!-- Loan Security Data -->
															<% if (loanSecurityCount > 0){ %>
															<tr class="<%=rowclass%>">
																<td colspan="10">
																	<table align="center" style="float:right;display:block;margin-top:-1%;width:98% !important;" class="tblDetails" width=95% border="0" rules="cols">
																		<thead class="th1">
																			<tr>
																				<th colspan="10">
																					<div class="frmTitle">
																						<h2 class="manualCPFormTitle">Loan Security Listing</h2>
															  						</div>
																				</th>
																			</tr>
																		</thead>
											
																		<tbody>
																			<!-- Detail Titles -->
																		    <tr class="headerD" style="text-align:center !important;">
																				<td width="5%" class="center_note">No.</td>
																				<td width="15%" class="center_note">Security ID</td>
																				<td width="15%" class="center_note">Security Type</td>
																				<td width="15%" class="center_note">Security Value</td>
																				<td width="15%" class="center_note" colspan="2" class="center_note">Action</td>
																			</tr>  
																		
																		<% for(int k = 0; k < vLoanSecurity.size(); k++){
																				LoanSecurityDao loanSecurityDao = (LoanSecurityDao) vLoanSecurity.elementAt(k);
																				String rowclass2 = ((k % 2) == 0 ? "odd":"even");		
																		%>	
																				
																			<tr class="<%=rowclass%>">										
																				<td class="center_note"><input name="facilityIndex" type="hidden" value="<%=loanSecurityDao.getFacilityIndex()%>" />
																				<input name="loanIndex" type="hidden" value="<%=k%>" />  <%=k+1%></td>
																				<td class="center_note"><input name=<%=j + "_securityId" + (k + 1) %> type="hidden" value="<%=loanSecurityDao.getSecurityID()%>" /><%=loanSecurityDao.getSecurityID()%></td>
																				<td class="center_note"><input name="<%=j + "_securityType" + (k + 1) %>" type="hidden" value="<%=loanSecurityDao.getSecurityType()%>" /><%=loanSecurityDao.getSecurityType()%></td>
																				<td class="center_note"><input name="<%=j + "_securityValue" + (k + 1) %>" type="hidden" value="<%=loanSecurityDao.getSecurityValue()%>" /><%=loanSecurityDao.getSecurityValue()%>
																				<input name="<%=j + "_loanSecurityDateTime" + (k + 1) %>" type="hidden" value="<%=loanSecurityDao.getDateTime()%>" /></td>
																				<td class="center_note">
																					<a href="javascript:editLoanData(<%=j%>, <%=k%>, 'loanSecurity')" >Edit</a> / 
																					<a href="javascript:deleteLoanData(<%=j%>, <%=k%>, 'loanSecurity')" >Delete</a>
																				</td>
																			</tr>
																		<% } // for loop (loanSecurity)  %>
																		
																		</tbody>
																	</table>
																</td></tr> <!-- ending tag -->
																<% }//if else (loanSecurityCount) 
 																		loanSecurityCount = 0; //reset count for loop
															}//for loop (facilityInfo)
													
														}//if else (facilityCount)
													
													}//if else (isNew)
													%>	
												
											</tbody>
										</table>
													
									
									
										

									<!-- Loan Borrower Listing -->				
										<table align="center" class="tblDetails" width=100% border="0" rules="cols" <%=vBorrowerDetails != null?"":"style=\"display:none\"" %>>
											<thead class="th1">
												<tr>
													<th colspan="9">
														<div class="frmTitle">
															<h2 class="manualCPFormTitle">Loan Borrower Listing</h2>
														</div>
													</th>
												</tr>
											</thead>
											
											<tbody>
												<!-- Detail Titles -->
											    <tr class="headerD" style="text-align:center !important;">
													<td width="5%" class="center_note">No.</td>
													<td width="7%" class="center_note">GCIF No.</td>
													<td width="14%" class="center_note">Capacity</td>
													<td width="14%" class="center_note">ID Type</td>
													<td width="14%" class="center_note">ID No.</td>
													<td width="14%" class="center_note">Other ID</td>
													<td width="14%" class="center_note">Name</td>
													<td width="14%" class="center_note">Percentage of Share</td>
													<td width="15%" class="center_note">Action</td>
												
												</tr>  
											
												
												<!-- BorrowerDetails Rows -->
													<% 
												
													if(isNew == true){
							
													}else{
														
														if (borrowerCount > 0 ){
															for(int j = 0; j < vBorrowerDetails.size(); j++){
																System.out.println(vBorrowerDetails.size());
																BorrowerDetailsDao borrowerDetails = (BorrowerDetailsDao) vBorrowerDetails.elementAt(j);	
																String rowclass = ((j % 2) == 0 ? "even":"odd");											
													
																%><tr class="<%=rowclass%>">
																<td><input name="borrowerIndex" type="hidden" value="<%=j%>" />  <%=j+1%></td>
																<td><input name="<%="gcifNo" + (j + 1) %>" type="hidden" value="<%=borrowerDetails.getGCIFNo()%>" /><%=borrowerDetails.getGCIFNo()%>-GCIF VALIDATED STATUS:<%=borrowerDetails.getCifStatus()%></td>
																<td><input name="<%="capacity" + (j + 1) %>" type="hidden" value="<%=borrowerDetails.getCapacity()%>" /><%=borrowerDetails.getCapacity()%></td>
																<td><input name="<%="idType" + (j + 1) %>" type="hidden" value="<%=borrowerDetails.getIDType()%>" /><%=borrowerDetails.getIDType()%></td>
																<td><input name="<%="idNo" + (j + 1) %>" type="hidden" value="<%=borrowerDetails.getIDNo()%>" /><%=borrowerDetails.getIDNo()%></td>
																
																<td><input name="<%="otherIdType" + (j + 1) %>" type="hidden" value="<%=borrowerDetails.getOtherIDType()%>" />
																<input name="<%="otherIdNo" + (j + 1) %>" type="hidden" value="<%=borrowerDetails.getOtherID()%>" /><%=borrowerDetails.getOtherID()%></td>
																<td><input name="<%="name" + (j + 1) %>" type="hidden" value="<%=borrowerDetails.getName()%>" /><%=borrowerDetails.getName()%></td>
																<td><input name="<%="percentage" + (j + 1) %>" type="hidden" value="<%=borrowerDetails.getPercentageOfShare()%>" /><%=borrowerDetails.getPercentageOfShare()%>
																<input name="<%="borrowerDateTime" + (j + 1) %>" type="hidden" value="<%=borrowerDetails.getDateTime()%>" /></td>
																<td>
																	<a href="javascript:editData(<%=j%>, 'borrowerDetails')" >Edit</a>
																	<a href="javascript:deleteData(<%=j%>, 'borrowerDetails')" >Delete</a>
																</td>
														</tr>
													<%
															}//for loop
													
														}//if else (borrowerCount)
													
													}//if else (isNew)
													%>
											</tbody>
										</table>
							
										
										<!-- Data Table Count -->
											<input name="borrowerCount" type="hidden" readonly value="<%=borrowerCount%>" />
											<input name="facilityCount" type="hidden" readonly value="<%=facilityCount%> " />
											<input name="loanSecurityCount" type="hidden" readonly value="<%=loanSecurityCount%> " />
										</form> <!-- end of frmManualCP -->	
						</div> <!--  moduleContainer END -->
							
					</div>
		
					<!-- Footer -->
					<%@ include file="/jsp/common/Footer.jsp" %>	
					<!-- End Footer -->
					</div>




</body>
</html>